package com.test.controller.admin;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.test.controller.Admininfo.MyObject;
import com.test.controller.Admininfo.ResData;
import com.test.entity.CwInfo;
import com.test.service.CwInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 财务(CwInfo)表控制层
 *
 * @author makejava
 * @since 2022-11-12 11:18:50
 */
@Controller
public class AdminCwInfoController {

    @Autowired
    private CwInfoService cwInfoService;

    //    显示财务页面信息
    @GetMapping("admin/cw_info")
    public String getCwInfo() {
        return "admin/cw_info/list";
    }

    //    显示财务管理信息
    @GetMapping("admin/cw_info/queryList")
    @ResponseBody
    public String getQueryList(Integer page, CwInfo cwInfo) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(cwInfoService.queryByPage(cwInfo, PageRequest.of(page - 1, 10)));
    }

    //    跳转财务新增页面
    @GetMapping("admin/cw_info/add")
    public String getAdd() {
        return "admin/cw_info/add_page";
    }

    //    提交财务新增
    @PostMapping("admin/cw_info/add_submit")
    @ResponseBody
    public ResData postAddSubmit(CwInfo cwInfo) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        cwInfo.setCreateTime(format.format(new Date()));
        ResData resData = new ResData();
        CwInfo insert = cwInfoService.insert(cwInfo);
        if (insert.getId() != null || !insert.getId().equals("")) {
            resData.setCode(200);
            resData.setMsg("添加成功");
            return resData;
        }
        resData.setCode(0);
        resData.setMsg("添加失败");
        return resData;
    }

    //    跳转财务修改页面
    @GetMapping("admin/cw_info/update")
    public ModelAndView getUpdate(Integer id) {
        ModelAndView modelAndView = new ModelAndView("admin/cw_info/update_page");
        CwInfo cwInfo = cwInfoService.queryById(id);
        modelAndView.addObject("data", cwInfo);
        return modelAndView;
    }

    //    提交财务修改页面信息
    @PostMapping("admin/cw_info/update_submit")
    @ResponseBody
    public ResData postUpdateSubmit(Integer id,CwInfo cwInfo) {
        cwInfo.setId(id);
        ResData resData = new ResData();
        CwInfo update = cwInfoService.update(cwInfo);
        if (update.getId() != null || !update.getId().equals("")) {
            resData.setCode(200);
            resData.setMsg("修改成功");
            return resData;
        }
        resData.setCode(200);
        resData.setMsg("修改成功");
        return resData;
    }

    //    删除财务页面信息
    @GetMapping("admin/cw_info/del")
    @ResponseBody
    public ResData getDelete(Integer id) {
        ResData resData = new ResData();
        boolean b = cwInfoService.deleteById(id);
        if (b) {
            resData.setCode(1);
            resData.setMsg("删除成功");
            return resData;
        }
        resData.setCode(0);
        resData.setMsg("删除失败");
        return resData;
    }

}

